﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using HeartLess.Managers;
using HeartLess.Core;

namespace HeartLess.Activities
{
    public class PauseActivity : Activity
    {

        private GameActivity _gameActivity;

        private Texture2D _background;
        
        private SpriteFont _menuFont;
        private int _selected = 1;

        private static String[] _menu = new String[]
        {
            "Paused",
            "Resume",
            "Quit Game"
        };
        private static Color _normalColor = Color.White;
        private static Color _titleColor = Color.Yellow;
        private static Color _selectColor = Color.Red;
        private static Color _bgColor = new Color(32, 32, 32, 128);

        public PauseActivity(GameActivity gameActivity)
        {
            InputManager.Input.DirectionalRepeatAllowed = false;

            _gameActivity = gameActivity;

           _background = new Texture2D(HeartlessGame.Game.GraphicsDevice,
                                       Screen.WINDOW_WIDTH,
                                       Screen.WINDOW_HEIGHT,
                                       false,
                                       SurfaceFormat.Color);

           Color[] color = new Color[Screen.WINDOW_WIDTH * Screen.WINDOW_HEIGHT];
            for (int i = 0; i < color.Length; i++)
            {
                color[i] = _bgColor;
            }

            _background.SetData(color);//set the color data on the texture

            _menuFont = AssetManager.GetSpriteFont(AssetManager.FONTS.MAIN_MENU);
        }

        public override void Update(GameTime gameTime)
        {
            if (InputManager.Input.IsPressingPause)
            {
                switch (_selected)
                {
                    case 1:
                        ResumeGame();
                        break;

                    case 2:
                        QuitGame();
                        break;
                }
            }
            else if (InputManager.Input.IsMovingDown)
                _selected++;
            else if (InputManager.Input.IsMovingUp)
                _selected--;
            if (_selected < 1)
                _selected = _menu.Count() - 1;
            else if (_selected >= _menu.Count())
                _selected = 1;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            _gameActivity.Draw(spriteBatch);

            spriteBatch.Draw(_background, Vector2.Zero, Color.White);

            // Draw each line equally spaced in the title safe area
            Viewport vp = spriteBatch.GraphicsDevice.Viewport;
            int height = vp.TitleSafeArea.Height;
            int offY = vp.TitleSafeArea.Y;                  // Offset to the title safe area
            int itemSp = height / (_menu.Count() + 1);      // Space for each menu line

            Vector2 stringWH = _menuFont.MeasureString(_menu[0]);
            spriteBatch.DrawString(_menuFont, _menu[0],
                new Vector2((vp.Width - stringWH.X) / 2, (itemSp - stringWH.Y) / 2 + offY),
                _titleColor);

            for (int i = 1; i < _menu.Count(); ++i)
            {
                stringWH = _menuFont.MeasureString(_menu[i]);
                spriteBatch.DrawString(_menuFont, _menu[i],
                    new Vector2((vp.Width - stringWH.X) / 2, (i + 1) * itemSp + (itemSp - stringWH.Y) / 2 + offY),
                    i == _selected ? _selectColor : _normalColor);
            }
        }

        private void ResumeGame()
        {
            InputManager.Input.DirectionalRepeatAllowed = true;
            ActivityManager.CurrentActivity = _gameActivity;
        }

        private void QuitGame()
        {
            HeartlessGame.Game.Exit();
        }
    }
}
